﻿using System;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using Data.Model;
using DataAccess.SystemManage;
using WinForm.Helper;

namespace WinForm.SystemManage
{
    public partial class FrmChangePassword : Form
    {
        public FrmChangePassword()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveSUser_Click(object sender, EventArgs e)
        {
            try
            {
                GC.Collect();
                var db = new SysUser();
                string userName = txtUserName.Text;
                string oldPwd = ParseMD5String.GetMD5Password(txtCurrentPwd.Text);
                string newPwd = ParseMD5String.GetMD5Password(txtNewPwd.Text);
                string newRePwd = ParseMD5String.GetMD5Password(txtReNewPwd.Text);
                int result = 0;
                if (string.IsNullOrWhiteSpace(txtUserName.Text))
                {
                    MyMessageBox.ShowMessageBoxInfo("必须输入用户名");
                    return;
                }
                if (string.IsNullOrWhiteSpace(txtCurrentPwd.Text))
                {
                    MyMessageBox.ShowMessageBoxInfo("必须输入当前密码");
                    return;
                }
                if (string.IsNullOrWhiteSpace(txtNewPwd.Text))
                {
                    MyMessageBox.ShowMessageBoxInfo("必须输入新密码");
                    return;
                }
                if (txtNewPwd.Text.Length < 4 || txtNewPwd.Text.Length > 16)
                {
                    MyMessageBox.ShowMessageBoxError("请输入4~16位英文或数字密码！");
                    return;
                }
                if (string.IsNullOrWhiteSpace(txtReNewPwd.Text))
                {
                    MyMessageBox.ShowMessageBoxInfo("必须重复输入新密码");
                    return;
                }
                if (txtNewPwd.Text.Trim() != txtReNewPwd.Text.Trim())
                {
                    MyMessageBox.ShowMessageBoxInfo("两次输入的新密码不一致，请重新输入");
                    return;
                }
                try
                {
                    ResultInfo<jsh_user> user = db.Login(userName,oldPwd);
                    if (user.ResultNumber == ResultEnum.Success)
                    {
                        user.Obj.password = newPwd;
                        result = db.CreateUpdateUser(user.Obj).Obj;
                        if (result > 0)
                        {
                            MyMessageBox.ShowMessageBoxInfo("修改密码成功！");
                            this.txtUserName.Clear();
                            this.txtCurrentPwd.Clear();
                            this.txtNewPwd.Clear();
                            this.txtReNewPwd.Clear();
                        }
                        else
                        {
                            MyMessageBox.ShowMessageBoxInfo("修改失败");
                        }
                    }
                    else
                    {
                        MyMessageBox.ShowMessageBoxInfo(user.Describe);
                    }
                }
                catch (Exception ex)
                {
                    MyMessageBox.ShowMessageBoxError(ex.Message);
                }
            }
            catch (Exception e1)
            {
                MyMessageBox.ShowMessageBoxError(e1.Message);
            }
            GC.Collect();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CloseChangeWindow();
        }

        private void CloseChangeWindow()
        {
            Close();
            Dispose();
        }
    }
}